# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/7/29
# @File   : TX07_ActualProfitWithIndustry_GZ.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:实际利润一级行业
code:TX07_ActualProfitWithIndustry_GZ
tableName:
columnName:
groups:税务模块
dependencies:TX_GZ_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    '''
    获取表
    :param x:
    :return:
    '''
    try:
        ic_data = TX_GZ_DSJ['data'].get(table_Name)
        if ic_data in null_type_list:
            return None
        else:
            return ic_data if isinstance(ic_data, list) and len(ic_data) > 0 else [{}]
    except:
        return None

def isfloat(x):
    '''
    判断x为float类型
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float):
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True', 'NAN', 'nan',
                     'False', '-inf', '-INF', '-INFINITY', '-infinity', 'NaN', 'Nan']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def Industry():
    try:
        syptGsdjxx = getTable('syptGsdjxx')
        if syptGsdjxx == [{}] or syptGsdjxx is None:
            return None
        else:
            HY_DM = syptGsdjxx[0].get('HY_DM')
            if isfloat(HY_DM) and HY_DM not in null_type_list:
                tmp = float(HY_DM[:2])
                if 51 <= tmp <= 52:
                    return '批发和零售业'
                elif 13 <= tmp <= 43:
                    return '制造业'
                else:
                    return '其他行业'
            else:
                return None
    except:
        return None

def Profit():
    try:
        syptQysdsA1 = getTable('syptQysdsA1')
        if syptQysdsA1 == [{}] or syptQysdsA1 is None:
            return None
        else:
            amt = {}
            flag = []
            count = 0
            for i in syptQysdsA1:
                ND = i.get('ND')
                YF = i.get('YF')
                tmp = i.get('SJLRE')
                if YF in ['03', '06', '09', '12']:
                    count += 1
                    if ND not in null_type_list and isfloat(ND) and isfloat(tmp):
                        amt.update({ND+'.'+YF:float(tmp)})
                    else:
                        flag.append(None)
                else:
                    pass
            if len(flag) == count:
                return None
            else:
                return sum(list(amt.values())) if amt != {} else None
    except:
        return None

def TX07_ActualProfitWithIndustry():
    try:
        f1 = Industry()
        f2 = Profit()
        if f1 is None or f2 is None:
            return u'缺失值'
        else:
            if f2 <= -5000000.0:
                return 1
            elif (f1 in ['制造业', '其他行业'] and -5000000.0 < f2 <= -270531.0) or (f1 == '其他行业' and -270531.0 < f2 <= 0):
                return 2
            elif f1 == '批发和零售业' and -5000000.0 < f2 <= 0:
                return 3
            elif (f1 in ['批发和零售业', '制造业', '其他行业'] and 0 < f2 <= 5000000.0) or (f1 == '制造业' and -270531.0 < f2 <= 0):
                return 4
            elif f2 > 5000000.0:
                return 5
            else:
                assert False
    except:
        return u'缺失值'

result = TX07_ActualProfitWithIndustry()